package netapp.udp_multicast;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class ServerDemo {
    public static void main(String[] args) throws IOException {
//        - 接收端
//        1. 创建接收端Socket对象(MulticastSocket)
        MulticastSocket socket = new MulticastSocket(12580);
//        2. 创建一个箱子,用于接收数据
        DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
//        3. 把当前计算机绑定一个组播地址
        socket.joinGroup(InetAddress.getByName("239.250.250.250"));
//        4. 将数据接收到箱子中
        while (true) {
            socket.receive(packet);
//        5. 解析数据包,并打印数据
            byte[] data = packet.getData();
            System.out.println(new String(data,0,data.length));
        }
//        6. 释放资源
//        socket.close();
    }
}
